package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.interest.entity.InterestCategoriesBean;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Jinni on 2017/12/1 0001
 */

public class InterestCategoriesDialogAdapter extends BaseRecyclerViewAdapter<InterestCategoriesBean.DataBean.NumberBean> {

    private Context mContext;

    /**
     * 被选中的tag位置
     */
    private int oldPosition = 0;

    /**
     * 用来存储被选中的item实体
     */
    private InterestCategoriesBean.DataBean.NumberBean mNumberBean;

    /**
     * item点击事件接口
     */
    private OnInterestCategoriesDialogItemClickListener mListener;

    public InterestCategoriesDialogAdapter(Context context) {
        mContext = context;
        mNumberBean = new InterestCategoriesBean.DataBean.NumberBean();
    }

    public int getOldPosition() {
        return oldPosition;
    }

    public void setOldPosition(int oldPosition) {
        this.oldPosition = oldPosition;
    }

    public InterestCategoriesBean.DataBean.NumberBean getNumberBean() {
        return mNumberBean;
    }

    public void setNumberBean(InterestCategoriesBean.DataBean.NumberBean numberBean) {
        mNumberBean = numberBean;
    }

    public OnInterestCategoriesDialogItemClickListener getListener() {
        return mListener;
    }

    public void setListener(OnInterestCategoriesDialogItemClickListener listener) {
        mListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder result;
        result = new VideoViewHolder(View.inflate(mContext, R.layout.item_interest_categories_dialog, null));
        return result;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        InterestCategoriesBean.DataBean.NumberBean bean = mList.get(position);
        if (holder instanceof VideoViewHolder) {
            ((VideoViewHolder) holder).setData(bean);
            ((VideoViewHolder) holder).setListener(bean);
        }
    }

    class VideoViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_category)
        TextView mTvCategory;

        VideoViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(InterestCategoriesBean.DataBean.NumberBean bean) {
            mTvCategory.setTextColor(ContextCompat.getColor(mContext, bean.isSelected() ?
                    R.color.interest_category_checked : R.color.interest_category_unchecked));
            Drawable drawable;
            if (bean.isSelected()) {
                drawable = ContextCompat.getDrawable(mContext, R.drawable.bg_angle_border_blue);
            } else {
                drawable = ContextCompat.getDrawable(mContext, R.drawable.bg_angle_border_gray);
            }
            mTvCategory.setBackground(drawable);
            mTvCategory.setText(bean.getName());
        }

        public void setListener(final InterestCategoriesBean.DataBean.NumberBean bean) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClickListener(bean, getAdapterPosition(), oldPosition);
                    }
                }
            });
        }
    }

    public interface OnInterestCategoriesDialogItemClickListener{
        void onItemClickListener(InterestCategoriesBean.DataBean.NumberBean bean, int position, int oldPosition);
    }
}
